package com.fanjun.learning.multithread.Thread_Per_Message_Pattern.miniServer;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by fanjun on 16/10/11.
 */
public class MiniServer {
    private final int portNum;
    public MiniServer(int port) {
        this.portNum = port;
    }

    public void execute() throws IOException {
        ServerSocket serverSocket = new ServerSocket(portNum);
        System.out.println("Listening on" + portNum);
        try {
            while (true){
                System.out.println("Accepting...");
                Socket socketClient = serverSocket.accept();
                System.out.println("Connect to " + socketClient);
                Service.service(socketClient);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            serverSocket.close();
        }
    }
}
